package com.yilin.medical.fragment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yilin.medical.R;
import com.yilin.medical.activity.BingLiGuanLiActivity;
import com.yilin.medical.activity.ModificationPasswordActivity;
import com.yilin.medical.activity.WanShanXinXiActivity;
import com.yilin.medical.activity.WoDeBingLiActivity;
import com.yilin.medical.activity.WoDeHuiZhenActivity;
import com.yilin.medical.activity.WoDeKeChengActivity;
import com.yilin.medical.activity.WoDeZhuanZhenActivity;
import com.yilin.medical.views.CircleImage;
import com.yilin.medical.views.MyAlertDialog;

/**
 * 个人中心fragment
 * 
 * @author lijianqun
 * 
 */
public class PersonalCenterFragment extends Fragment implements OnClickListener {
	private Context context;
	private View mView;
	private CircleImage mCircleImagePicture;
	private RelativeLayout relativeLayoutCourse, relativeLayoutBingLi,
			relativeLayoutHuiZhen, relativeLayoutZhuanZhen,
			relativeLayoutModificationPassword, relativeLayoutZhiHuiBiGuanLi;
	private TextView textViewName;
	private static final int NONE = 0;
	private static final int PHOTO_GRAPH = 1;// 拍照
	private static final int PHOTO_ZOOM = 2; // 缩放
	private static final int PHOTO_RESOULT = 3;// 结果
	private static final String IMAGE_UNSPECIFIED = "image/*";

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		context = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = View.inflate(context, R.layout.fragment_personalcenter, null);
		initView();
		registerLintener();
		return mView;
	}

	private void initView() {
		textViewName = (TextView) mView.findViewById(R.id.mTextViewMyName);
		relativeLayoutCourse = (RelativeLayout) mView
				.findViewById(R.id.rl_course);
		relativeLayoutBingLi = (RelativeLayout) mView
				.findViewById(R.id.rl_bingli);
		relativeLayoutHuiZhen = (RelativeLayout) mView
				.findViewById(R.id.rl_huizhen);
		relativeLayoutZhuanZhen = (RelativeLayout) mView
				.findViewById(R.id.rl_zhuanzhen);
		relativeLayoutModificationPassword = (RelativeLayout) mView
				.findViewById(R.id.rl_modificationpassword);
		relativeLayoutZhiHuiBiGuanLi = (RelativeLayout) mView
				.findViewById(R.id.rl_zhihuibi_guanli);
		mCircleImagePicture = (CircleImage) mView
				.findViewById(R.id.CircleImage_picture);
	}

	private void registerLintener() {
		relativeLayoutCourse.setOnClickListener(this);
		relativeLayoutBingLi.setOnClickListener(this);
		relativeLayoutHuiZhen.setOnClickListener(this);
		relativeLayoutZhuanZhen.setOnClickListener(this);
		relativeLayoutModificationPassword.setOnClickListener(this);
		relativeLayoutZhiHuiBiGuanLi.setOnClickListener(this);
		mCircleImagePicture.setOnClickListener(this);
		// textViewName.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.rl_course:
			// TODO 我的课程
			intent = new Intent(context, WoDeKeChengActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_bingli:
			// TODO 我的病例
			intent = new Intent(context, WoDeBingLiActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_huizhen:
			// TODO 我的会诊
			intent = new Intent(context, WoDeHuiZhenActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_zhuanzhen:
			// TODO 我的转诊
			intent = new Intent(context, WoDeZhuanZhenActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_modificationpassword:
			// TODO 修改密码
			intent = new Intent(context, ModificationPasswordActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_zhihuibi_guanli:
			intent = new Intent(context, BingLiGuanLiActivity.class);
			startActivity(intent);
			// TODO 病例管理
			break;
		case R.id.CircleImage_picture:
			//TODO 临时入口,进入完善信息页面,或者弹出修改头像的dialog
			Random random = new Random();
			int tempNO = random.nextInt(2);
			if (tempNO == 0) {
				intent = new Intent(context, WanShanXinXiActivity.class);
				startActivity(intent);
			} else {
				showDialog();
			}

			break;
		}
	}

	private void showDialog() {
		final MyAlertDialog alertDialog = new MyAlertDialog(context);
		alertDialog.setListenerCamera(new OnClickListener() {
			// 相机
			@Override
			public void onClick(View v) {
				// 从拍照获取图片
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
						Environment.getExternalStorageDirectory(), "temp.jpg")));
				startActivityForResult(intent, PHOTO_GRAPH);
				alertDialog.dismiss();
			}
		});
		alertDialog.setListenerSd(new OnClickListener() {
			// sd卡
			@Override
			public void onClick(View v) {
				// 从相册获取图片
				Intent intent = new Intent(Intent.ACTION_PICK, null);
				intent.setDataAndType(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						IMAGE_UNSPECIFIED);
				startActivityForResult(intent, PHOTO_ZOOM);
				alertDialog.dismiss();

				alertDialog.dismiss();
			}
		});
		alertDialog.setCamera("拍照");
		alertDialog.setListenerCancle(new OnClickListener() {
			// 取消
			@Override
			public void onClick(View v) {

				alertDialog.dismiss();
			}
		});
		alertDialog.setCancle("取消");
		alertDialog.setSd("从相册");
		alertDialog.show();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == NONE)
			return;
		// 拍照
		if (requestCode == PHOTO_GRAPH) {
			// 设置文件保存路径
			File picture = new File(Environment.getExternalStorageDirectory()
					+ "/temp.jpg");
			startPhotoZoom(Uri.fromFile(picture));
		}

		if (data == null)
			return;

		// 读取相册缩放图片
		if (requestCode == PHOTO_ZOOM) {
			startPhotoZoom(data.getData());
		}
		// 处理结果
		if (requestCode == PHOTO_RESOULT) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)压缩文件
				// 此处可以把Bitmap保存到sd卡中，http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html
				mCircleImagePicture.setImageBitmap(photo); // 把图片显示在ImageView控件上
			}

		}

		super.onActivityResult(requestCode, requestCode, data);
	}

	/**
	 * 收缩图片
	 * 
	 * @param uri
	 */
	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 300);
		intent.putExtra("outputY", 500);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTO_RESOULT);
	}

}
